﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace baseClass.Forms
{
    public partial class baseReport : baseForm 
    {
        public baseReport()
        {
            InitializeComponent();
            this.myOnErrorEvent += new myOnError(application.SysLibs.ErrorHandler);
        }
        protected bool myFullMode
        {
            get
            {
                return myMainTabControl.Visible;
            }
            set
            {
                if (common.Settings.sysIsDesignMode) return; 
                myMainTabControl.Visible = value;
                SetFullMode(value);
                FullModeChanged();
                Application.DoEvents();
            }
        }

        protected virtual bool DataValid()
        {
            return true;
        }
        protected virtual void ViewReport(){}
        protected virtual void PrintReport(){}

        int lastContainerHeight = -1;
        protected virtual void SetFullMode(bool value)
        {
            if (value && this.WindowState == FormWindowState.Maximized)
            {
                myMainTabControl.Size = new Size(this.Width,this.Height - myMainTabControl.Location.Y -  2 * SystemInformation.CaptionHeight-5);
                return;
            }
            if (value && lastContainerHeight>0) myMainTabControl.Height = lastContainerHeight;
            if (value == false && myMainTabControl.Visible) lastContainerHeight = myMainTabControl.Height;

            myMainTabControl.Anchor = AnchorStyles.Top | AnchorStyles.Left;
            myMainTabControl.Width = this.Width-myMainTabControl.Location.X -7;
            this.Height = myMainTabControl.Location.Y + (value ? myMainTabControl.Height : 10) + 2 * SystemInformation.CaptionHeight + 8;
            myMainTabControl.Anchor = (value ? AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom : AnchorStyles.Top | AnchorStyles.Left);

            if (value && myMainTabControl.Height < 30)
            {
                this.Height += 300;
            }
        }
        protected virtual void FullModeChanged() { }

        protected override bool BeforeLoadForm() 
        {
            myFullMode = false;
            return true; 
        }
      
        private void viewBtn_Click(object sender, EventArgs e)
        {
            try
            {
                this.ShowMessage("");
                this.ShowReccount("");
                if (!DataValid()) return;
                this.ShowCurrorWait();
                viewBtn.Enabled = false;
                myFullMode = false;
                ViewReport();
                myFullMode = true;
            }
            catch (Exception er)
            {
                this.ShowCurrorDefault();
                this.ErrorHandler(this, er);
            }
            finally
            {
                viewBtn.Enabled = true;
                this.ShowCurrorDefault();
            }
        }
        private void printBtn_Click(object sender, EventArgs e)
        {
            try
            {
                this.ShowMessage("");
                this.ShowReccount("");
                if (!DataValid()) return;

                this.WindowState = FormWindowState.Normal;
                this.ShowCurrorWait();
                printBtn.Enabled = false;
                this.myFullMode = false;
                PrintReport();
            }
            catch (Exception er)
            {
                this.ShowCurrorDefault();
                this.ErrorHandler(this, er);
            }
            finally
            {
                printBtn.Enabled = true;
                this.ShowCurrorDefault();
            }
        }
        private void exitBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
